<?php

namespace App\Admin\Controllers;

use App\UserTeam;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UserTeamController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户等级');
            $content->description('用户等级');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改用户等级');
            $content->description('修改用户等级');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户等级增加');
            $content->description('用户等级增加');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserTeam::class, function (Grid $grid) {
            $grid->model()->where('level','!=',0);
            $grid->id('ID')->sortable();
            $grid->name('等级名称');
            $grid->user_num('直推会员数量条件');
            $grid->user_money('推广团队收益总额条件(元)');
            $grid->reward('团队分成比例%');
            $grid->level('会员级别');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserTeam::class, function (Form $form) {
            $form->text('name', '等级名称')->placeholder('请输入等级名称')->rules('required', [
                'required' => '登记名称不能为空',
            ]);
            $form->number('user_num', '直推会员数量满足条件')->placeholder('输入升级会员所需的直推会员数')->rules('required',
                [
                    'required' => '直推会员数不能为空',
                ]);
            $form->number('reward', '团队分成比例(%)')->rules('required|integer|between:0,100',[
                'required'=>'团队分成比例不能为空',
                'between'=>'团队分成比例在0到100之间'
            ])->help('团队分成比例在0到100之间，按照百分比');
            $form->currency('user_money', '推广团队收益总额条件(元)')->symbol('元')->value(0)->placeholder('输入升级会员推广团队收益');
            $form->number('level', '会员级别')->value(0)->placeholder('会员等级用以系统判断级别高低')->rules(function ($form) {
                if (!isset($form->model()->id)) {
                    return 'unique:user_teams,level';
                }

            }, [
                    'unique' => '会员等级必须唯一'
                ]
            );

        });
    }
}
